<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" @submit.native.prevent>
      <el-form-item label="关键字" prop="search" label-width="68px">
        <el-input v-model="queryParams.search" placeholder="名称、详细名称" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否上架" prop="isPutaway">
        <el-select v-model="queryParams.isPutaway" clearable style="width: 140px">
          <el-option v-for="dict in this.putawayStatusList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" v-hasPermi="['luckDraw:prize:query']">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['luckDraw:prize:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :height="tableHeight" v-loading="loading" :data="prizeList" stripe border>
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column label="奖品名称" prop="name" />
      <el-table-column label="详细名称" prop="detailName" width="250" />
      <el-table-column label="图片" width="120">
        <template slot-scope="scope">
          <table-image :imgDicUrls="scope.row.img"></table-image>
        </template>
      </el-table-column>
      <el-table-column label="商品类型" width="120" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.type == 1?'实物':'虚拟'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="奖品简介" prop="intro" width="300" />
      <el-table-column label="奖品概率" prop="percent" align="center" />
      <el-table-column label="库存" prop="stock" align="center" />

      <el-table-column label="排序" prop="orderNum" align="center" />
      <el-table-column label="奖品价值" prop="price" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.price}}{{scope.row.type== 0?'(数量)':'元'}} </span>
        </template>
      </el-table-column>
      <el-table-column label="是否上架" prop="isPutaway" align="center">
        <template slot-scope="scope">
          <span> {{selectDictLabel(isPutaway,scope.row.isPutaway)}} </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['luckDraw:prize:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click.stop="handleDelete(scope.row)" v-hasPermi="['luckDraw:prize:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <z-dialog :title="title" :visible.sync="open" @confirm="submitForm">
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-form-item label="奖品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入奖品名称" class="input-W" />
        </el-form-item>
        <el-form-item label="奖品类型" prop="type">
          <el-select v-model="form.type" placeholder="类型选择" clearable class="input-W" @change="typeChange">
            <el-option v-for="dict in dict.type.goods_prize_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>

        <el-form-item v-if="form.type == 0" label="虚拟奖品" prop="secCode">
          <el-select v-model="form.secCode" placeholder="类型选择" clearable class="input-W" @change="$forceUpdate()">
            <el-option v-for="dict in secCode" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="详细名称" prop="detailName">
          <el-input v-model="form.detailName" placeholder="请输入详细名称" class="input-W" />
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <image-upload :valueData="form.img" params="img" @uploaded="uploaded"></image-upload>
        </el-form-item>
        <el-form-item label="奖品简介" prop="intro">
          <el-input v-model="form.intro" type="textarea" placeholder="请输入内容" class="input-W"></el-input>
        </el-form-item>
        <el-form-item label="奖品概率" prop="percent">
          <el-input v-model="form.percent" placeholder="请输入详细概率" class="input-W" />%
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" class="input-W" />
        </el-form-item>
        <el-form-item :label="form.type == 0?'奖品数量':'奖品价值'" prop="price">
          <el-input v-model="form.price" placeholder="请输入奖品价值" class="input-W" />*说明:虚拟奖品填写数量
        </el-form-item>
        <el-form-item label="是否上架" prop="isPutaway">
          <el-select v-model="form.isPutaway" placeholder="是否上架" clearable class="input-W">
            <el-option v-for="dict in this.isPutaway" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="1" class="input-W" />
        </el-form-item>
      </el-form>
    </z-dialog>
  </div>
</template>

<script>
import tableImage from "@/components/TableImageView/index.vue";
import { listGoodsprize, editGoodsprize, addGoodsprize, delGoodsprize } from "@/api/luckdraw/prize";
import { deepClone } from '@/utils/index'

export default {
  components: { tableImage },
  name: "prize",
  dicts: [
    "goods_prize_type",
    "goods_putaway_status",
  ],
  data() {
    return {

      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      prizeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
        search: undefined,
      },

      secCode: [
        { label: '金积分', value: 'GOLD' },
        { label: '银积分', value: 'SLV' },
        { label: '金牌席位', value: 'GVIP' },
        { label: '兑换券', value: 'CONSUME' }

      ],

      isPutaway: [{ label: '上架', value: 1 }, { label: '下架', value: 0 }],

      // 表单参数
      form: {},
      rules: {
        name: [{ required: true, message: "必填", trigger: "blur" }],
        img: [{ required: true, message: "必填", trigger: "blur" }],
        percent: [{ required: true, message: "必填", trigger: "blur" }],
        type: [{ required: true, message: "必填", trigger: "blur" }],
        intro: [{ required: true, message: "必填", trigger: "blur" }],
        detailName: [{ required: true, message: "必填", trigger: "blur" }],
        price: [{ required: true, message: "必填", trigger: "blur" }],
        secCode: [{ required: false, message: "必填", trigger: "blur" }],
        // percent: [{ required: true, message: "必填", trigger: "blur" }],
        // percent: [{ required: true, message: "必填", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 280px)'
    },
    prizeTypeList() {
      let typeDictArray = JSON.parse(
        JSON.stringify(this.dict.type.goods_prize_type)
      );
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
     //上架状态
     putawayStatusList() {
      let typeDictArray = JSON.parse(
        JSON.stringify(this.dict.type.goods_putaway_status)
      );
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
  },
  methods: {
    getList() {
      this.loading = true;
      listGoodsprize(this.queryParams)
        .then((response) => {
          if (response.code == 200) {
            this.prizeList = response.data;
            this.total = response.total;
          }
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {}
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    //** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增奖品";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "编辑奖品";
      this.form = deepClone(row);
      this.form.type = String(row.type);

      if (this.form.type == 1) {
        this.rules.secCode[0].required = false;
      } else {
        this.rules.secCode[0].required = true;
      }
    },

    /** 提交按钮 */
    submitForm: function () {

      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            editGoodsprize(this.form).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoodsprize(this.form).then((response) => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除奖品"' + row.name + '"的数据项？')
        .then(() => {
          delGoodsprize({ goodsPrizeId: row.id }).then((response) => {
            this.$modal.msgSuccess("删除成功");
            this.getList();
          });
        }
        ).catch(() => { });
    },

    //商品类型切管
    typeChange(a) {
      this.form.secCode = undefined;
      if (a == 1) {
        this.rules.secCode[0].required = false;
      } else {
        this.rules.secCode[0].required = true;
      }
    },


    /**上传数据 */
    uploaded(params, dict) {
      this.form[params] = JSON.stringify(dict);
    },
  },
};
</script>
<style scoped>
.input-W {
  width: 240px;
}
</style>
